package models.data;

import models.general.vKennariHoparOnn;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import play.mvc.Http;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: olgak
 * Date: 6.3.2013
 * Time: 13:46
 * To change this template use File | Settings | File Templates.
 */
@Entity
public class Spurningar extends Model {

    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy");


    @Id
    //@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "s_spurningar")
    public Integer spurningar_id;
    @Constraints.Required
    public String tegund;
    public int onn_id;
    @Constraints.Required
    public String texti;
    @Constraints.Required
    public String afangi_id;

    public int tyngdarstig;
    public String kennari_id;

    @Formats.DateTime(pattern="yyyy-MM-dd")
    public Date dagurstofnad;

    //use if question is changed
    public Date dagurbreytt;
    public int spurningbreytt_id;

    public String bok_id;
    public String leitarskilyrdi;
    public String kafli;
    public String bok_nafn;

    @Transient
    public Http.MultipartFormData.FilePart vidhengi;
    public String isbn;
    public String nr_utgafu;

    public String hofundur_bokar;

    public Spurningar(String tegund, String texti, int onn_id, String afangi_id, int tyngdarstig) {
        this.spurningar_id = createId();
        this.tegund = tegund;
        this.texti = texti;
        this.onn_id = onn_id;
        this.afangi_id = afangi_id;
        this.tyngdarstig = tyngdarstig;


    }

    public Spurningar(Date dagurstofnad, String kennari_id, String afangi_id, int tyngdarstig, String texti, int onn_id, String tegund, String bok_id, String kafli, String nr_utgafu, String hofundur_bokar, Http.MultipartFormData.FilePart  vidhengi, String bok_nafn) {
        this.spurningar_id = createId();
        this.dagurstofnad = dagurstofnad;
        this.kennari_id = kennari_id;
        this.afangi_id = afangi_id;
        this.tyngdarstig = tyngdarstig;
        this.texti = texti;
        this.onn_id = onn_id;
        this.tegund = tegund;
        this.bok_id = bok_id;
        this.kafli = kafli;
        this.nr_utgafu = nr_utgafu;
        this.hofundur_bokar = hofundur_bokar;
        this.vidhengi = vidhengi;
        this.bok_nafn = bok_nafn;
    }

    public Spurningar(Date dagurstofnad, String kennari_id, String afangi_id, int tyngdarstig, String texti, int onn_id, String tegund, String bok_id, String kafli, String nr_utgafu, String hofundur_bokar, String bok_nafn) {
        this.spurningar_id = createId();
        this.dagurstofnad = dagurstofnad;
        this.kennari_id = kennari_id;
        this.afangi_id = afangi_id;
        this.tyngdarstig = tyngdarstig;
        this.texti = texti;
        this.onn_id = onn_id;
        this.tegund = tegund;
        this.bok_id = bok_id;
        this.kafli = kafli;
        this.nr_utgafu = nr_utgafu;
        this.hofundur_bokar = hofundur_bokar;
        this.bok_nafn=bok_nafn;
    }

    public Spurningar(String tegund, Date dagurstofnad, int tyngdarstig, String afangi_id, String texti, int onn_id) {
        this.spurningar_id = createId();
        this.tegund = tegund;
        this.dagurstofnad = dagurstofnad;
        this.tyngdarstig = tyngdarstig;
        this.afangi_id = afangi_id;
        this.texti = texti;
        this.onn_id = onn_id;
    }

    //used when creating question without book but with attachment
    public Spurningar(String kennari_id, String tegund, Date dagurstofnad, int tyngdarstig, String afangi_id, String texti, int onn_id,Http.MultipartFormData.FilePart  vidhengi) {
        this.spurningar_id = createId();
        this.kennari_id = kennari_id;
        this.tegund = tegund;
        this.dagurstofnad = dagurstofnad;
        this.tyngdarstig = tyngdarstig;
        this.afangi_id = afangi_id;
        this.texti = texti;
        this.onn_id = onn_id;
        this.vidhengi=vidhengi;
    }

    public static Spurningar create(String tegund, String texti, int onn_id, String afangi_id, int tyngdarstig, String kennari_id, String bok_id, String kafli, String nr_utgafu, String hofundur_bokar, Http.MultipartFormData.FilePart  vidhengi, String bok_nafn ){

        Date date = new Date();
        Spurningar spurning = new Spurningar(date, kennari_id, afangi_id, tyngdarstig, texti, onn_id, tegund, bok_id, kafli, nr_utgafu, hofundur_bokar, vidhengi,bok_nafn);
        spurning.save();
        return spurning;

    }

    public static Spurningar createTextQuesitonWOAttachment(String tegund, String texti, int onn_id, String afangi_id, int tyngdarstig, String kennari_id, String bok_id, String kafli, String nr_utgafu, String hofundur_bokar, String bok_nafn ){

        Date date = new Date();
        Spurningar spurning = new Spurningar(date, kennari_id, afangi_id, tyngdarstig, texti, onn_id, tegund, bok_id, kafli, nr_utgafu, hofundur_bokar, bok_nafn);
        spurning.save();
        return spurning;

    }

    public static Spurningar createWithoutVidhengi(String tegund, String texti, int onn_id, String afangi_id, int tyngdarstig, String kennari_id, String bok_id, String kafli, String nr_utgafu, String hofundur_bokar, String bok_nafn){

        Date date = new Date();
        Spurningar spurning = new Spurningar(date, kennari_id, afangi_id, tyngdarstig, texti, onn_id, tegund, bok_id, kafli, nr_utgafu, hofundur_bokar, bok_nafn);
        spurning.save();
        return spurning;

    }

    public static Finder<Integer,Spurningar> find = new Finder<Integer,Spurningar>(
            Integer.class, Spurningar.class
    );

    public static Spurningar createWithoutBook(String tegund, String texti, int onn_id, String afangi_id, int tyngdarstig, String kennari_id,Http.MultipartFormData.FilePart  vidhengi ){

        Date date = new Date();
        Spurningar spurning = new Spurningar(kennari_id,tegund,date, tyngdarstig,afangi_id, texti, onn_id,vidhengi);
        spurning.save();
        return spurning;

    }

    public static List<Spurningar> findByVerkSpurn(List<Verkefnaspurningar> verkspurn){
        List<Spurningar> spurn = new ArrayList<Spurningar>();
        for(Verkefnaspurningar vSpurn : verkspurn){
            Spurningar s = Spurningar.find.byId(vSpurn.spurningar_id);
            spurn.add(s);
        }
        return spurn;
    }

    public static List<Spurningar> findAllSpurningarByAfangiList(List<vKennariHoparOnn> classes){
        List<Spurningar> returnList = new ArrayList<Spurningar>();
        for(vKennariHoparOnn single : classes){
            List<Spurningar> temp = Spurningar.findByAfangiId(Integer.toString(single.afangi_id));
            returnList.addAll(temp);
        }
        return returnList;
    }

    public static Spurningar changeQuestion(Integer spurning_id,String texti){
        Spurningar spurning = Spurningar.find.byId(spurning_id);
        spurning.setTexti(texti);
        spurning.save();
        return spurning;
    }

    public static List<Spurningar> getSpurningarByAfangi(String afangiId){

        List<Spurningar> spurningar = Spurningar.find.where().eq("afangi_id" , afangiId).findList();
        return spurningar;
    }

    public static List<Spurningar> findByAfangiId(String afangi_id){
        if(Spurningar.find.where().eq("afangi_id", afangi_id).findList().equals(null)){
            List<Spurningar> spurn = new ArrayList<Spurningar>();
            return spurn;
        }
        else{
            return Spurningar.find.where().eq("afangi_id", afangi_id).findList();
        }
    }

    public Integer getSpurningar_id() {
        return spurningar_id;
    }

    public void setSpurningar_id(Integer spurningar_id) {
        this.spurningar_id = spurningar_id;
    }
    public static Spurningar getByID(int spurningar_id){
        return find.byId(spurningar_id);
    }

    public static Integer createId(){
        List<Object> spurningar = Spurningar.find.findIds();

        Integer max = 1;
        for(Object verk : spurningar){
            if(Integer.valueOf(verk.toString()) > max) {
                max = Integer.valueOf(verk.toString());
            }
        }
        return (max+1);
    }

    public String getTegund() {
        return tegund;
    }

    public void setTegund(String tegund) {
        this.tegund = tegund;
    }

    public int getOnn_id() {
        return onn_id;
    }

    public void setOnn_id(int onn_id) {
        this.onn_id = onn_id;
    }

    public String getTexti() {
        return texti;
    }

    public void setTexti(String texti) {
        this.texti = texti;
    }

    public String getAfangi_id() {
        return afangi_id;
    }

    public void setAfangi_id(String afangi_id) {
        this.afangi_id = afangi_id;
    }

    public int getTyngdarstig() {
        return tyngdarstig;
    }

    public void setTyngdarstig(int tyngdarstig) {
        this.tyngdarstig = tyngdarstig;
    }

    public String getKennari_id() {
        return kennari_id;
    }

    public void setKennari_id(String kennari_id) {
        this.kennari_id = kennari_id;
    }

    public Date getDagurstofnad() {
        return dagurstofnad;
    }

    public void setDagurstofnad(Date dagurstofnad) {
        this.dagurstofnad = dagurstofnad;
    }

    public Date getDagurbreytt() {
        return dagurbreytt;
    }

    public void setDagurbreytt(Date dagurbreytt) {
        this.dagurbreytt = dagurbreytt;
    }

    public int getSpurningbreytt_id() {
        return spurningbreytt_id;
    }

    public void setSpurningbreytt_id(int spurningbreytt_id) {
        this.spurningbreytt_id = spurningbreytt_id;
    }

    public String getBok_id() {
        return bok_id;
    }

    public void setBok_id(String bok_id) {
        this.bok_id = bok_id;
    }

    public String getLeitarskilyrdi() {
        return leitarskilyrdi;
    }

    public void setLeitarskilyrdi(String leitarskilyrdi) {
        this.leitarskilyrdi = leitarskilyrdi;
    }

    public String getKafli() {
        return kafli;
    }

    public void setKafli(String kafli) {
        this.kafli = kafli;
    }

    public Http.MultipartFormData.FilePart getVidhengi() {
        return vidhengi;
    }

    public void setVidhengi(Http.MultipartFormData.FilePart vidhengi) {
        this.vidhengi = vidhengi;
    }

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public String getNr_utgafu() {
        return nr_utgafu;
    }

    public void setNr_utgafu(String nr_utgafu) {
        this.nr_utgafu = nr_utgafu;
    }

    public String getHofundur_bokar() {
        return hofundur_bokar;
    }

    public void setHofundur_bokar(String hofundur_bokar) {
        this.hofundur_bokar = hofundur_bokar;
    }
}